home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / base / netkit-b.07a / netkit-b / NetKit-B-0.07A / timed / timedc / cmds.c next >
Encoding:
C/C++ Source or Header  |  1996-07-20  |  12.6 KB  |  521 lines

  1. /*-
  2.  * Copyright (c) 1985, 1993 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. /*
  35.  * From: @(#)cmds.c    5.1 (Berkeley) 5/11/93
  36.  */
  37. char cmds_rcsid[] =
  38.   "$Id: cmds.c,v 1.3 1996/07/20 19:45:11 dholland Exp $";
  39.  
  40. #ifdef sgi
  41. #ident "$Revision: 1.3 $"
  42. #endif
  43.  
  44. #include "timedc.h"
  45. #include <sys/file.h>
  46.  
  47. #include <netinet/ip.h>
  48. #include <netinet/ip_icmp.h>
  49.  
  50. #include <stdlib.h>
  51. #include <strings.h>
  52. #include <unistd.h>
  53.  
  54. #define TSPTYPES
  55. #include <protocols/timed.h>
  56.  
  57. #ifdef sgi
  58. #include <bstring.h>
  59. #include <sys/clock.h>
  60. #else
  61. #define    SECHR    (60*60)
  62. #define    SECDAY    (24*SECHR)
  63. #endif /* sgi */
  64.  
  65. # define DATE_PROTO "udp"
  66. # define DATE_PORT "time"
  67.  
  68.  
  69. int sock;
  70. int sock_raw;
  71. char myname[MAXHOSTNAMELEN];
  72. struct hostent *hp;
  73. struct sockaddr_in server;
  74. struct sockaddr_in dayaddr;
  75. extern int measure_delta;
  76.  
  77. void bytenetorder(struct tsp *);
  78. void bytehostorder(struct tsp *);
  79.  
  80.  
  81. #define BU ((unsigned long)2208988800U)    /* seconds before UNIX epoch */
  82.  
  83.  
  84. /* compute the difference between our date and another machine
  85.  */
  86. static int                /* difference in days from our time */
  87. daydiff(char *hostname)
  88. {
  89.     int i;
  90.     int trials;
  91.     struct timeval tout, now;
  92.     fd_set ready;
  93.     struct sockaddr from;
  94.     int fromlen;
  95.     unsigned long sec;
  96.  
  97.  
  98.     /* wait 2 seconds between 10 tries */
  99.     tout.tv_sec = 2;
  100.     tout.tv_usec = 0;
  101.     for (trials = 0; trials < 10; trials++) {
  102.         /* ask for the time */
  103.         sec = 0;
  104.         if (sendto(sock, &sec, sizeof(sec), 0,
  105.                (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
  106.             perror("sendto(sock)");
  107.             return 0;
  108.         }
  109.  
  110.         for (;;) {
  111.             FD_ZERO(&ready);
  112.             FD_SET(sock, &ready);
  113.             i = select(sock+1, &ready, (fd_set *)0,
  114.                    (fd_set *)0, &tout);
  115.             if (i < 0) {
  116.                 if (errno == EINTR)
  117.                     continue;
  118.                 perror("select(date read)");
  119.                 return 0;
  120.             }
  121.             if (0 == i)
  122.                 break;
  123.  
  124.             fromlen = sizeof(from);
  125.             if (recvfrom(sock,&sec,sizeof(sec),0,
  126.                      &from,&fromlen) < 0) {
  127.                 perror("recvfrom(date read)");
  128.                 return 0;
  129.             }
  130.  
  131.             sec = ntohl(sec);
  132.             if (sec < BU) {
  133.                 fprintf(stderr,
  134.                     "%s says it is before 1970: %lu",
  135.                     hostname, sec);
  136.                 return 0;
  137.             }
  138.             sec -= BU;
  139.  
  140.             (void)gettimeofday(&now, (struct timezone*)0);
  141.             return (sec - now.tv_sec);
  142.         }
  143.     }
  144.  
  145.     /* if we get here, we tried too many times */
  146.     fprintf(stderr,"%s will not tell us the date\n", hostname);
  147.     return 0;
  148. }
  149.  
  150.  
  151. /*
  152.  * Clockdiff computes the difference between the time of the machine on
  153.  * which it is called and the time of the machines given as argument.
  154.  * The time differences measured by clockdiff are obtained using a sequence
  155.  * of ICMP TSTAMP messages which are returned to the sender by the IP module
  156.  * in the remote machine.
  157.  * In order to compare clocks of machines in different time zones, the time
  158.  * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
  159.  * If a hosts uses a different time format, it should set the high order
  160.  * bit of the 32-bit quantity it transmits.
  161.  * However, VMS apparently transmits the time in milliseconds since midnight
  162.  * local time (rather than GMT) without setting the high order bit.
  163.  * Furthermore, it does not understand daylight-saving time.  This makes
  164.  * clockdiff behaving inconsistently with hosts running VMS.
  165.  *
  166.  * In order to reduce the sensitivity to the variance of message transmission
  167.  * time, clockdiff sends a sequence of messages.  Yet, measures between
  168.  * two `distant' hosts can be affected by a small error. The error can,
  169.  * however, be reduced by increasing the number of messages sent in each
  170.  * measurement.
  171.  */
  172. void
  173. clockdiff(argc, argv)
  174.     int argc;
  175.     char *argv[];
  176. {
  177.     int measure_status;
  178.     extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
  179.     register int avg_cnt;
  180.     register long avg;
  181.     struct servent *sp;
  182.  
  183.     if (argc < 2)  {
  184.         printf("Usage: clockdiff host ... \n");
  185.         return;
  186.     }
  187.  
  188.     (void)gethostname(myname,sizeof(myname));
  189.  
  190.     /* get the address for the date ready */
  191.     sp = getservbyname(DATE_PORT, DATE_PROTO);
  192.     if (!sp) {
  193.         (void)fprintf(stderr, "%s/%s is an unknown service\n",
  194.                   DATE_PORT, DATE_PROTO);
  195.         dayaddr.sin_port = 0;
  196.     } else {
  197.         dayaddr.sin_port = sp->s_port;
  198.     }
  199.  
  200.     while (argc > 1) {
  201.         argc--; argv++;
  202.         hp = gethostbyname(*argv);
  203.         if (hp == NULL) {
  204.             fprintf(stderr, "timedc: %s: ", *argv);
  205.             herror(0);
  206.             continue;
  207.         }
  208.  
  209.         server.sin_family = hp->h_addrtype;
  210.         bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
  211.         for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
  212.             measure_status = measure(10000,100, *argv, &server, 1);
  213.             if (measure_status != GOOD)
  214.                 break;
  215.             avg += measure_delta;
  216.         }
  217.         if (measure_status == GOOD)
  218.             measure_delta = avg/avg_cnt;
  219.  
  220.         switch (measure_status) {
  221.         case HOSTDOWN:
  222.             printf("%s is down\n", hp->h_name);
  223.             continue;
  224.         case NONSTDTIME:
  225.             printf("%s transmitts a non-standard time format\n",
  226.                    hp->h_name);
  227.             continue;
  228.         case UNREACHABLE:
  229.             printf("%s is unreachable\n", hp->h_name);
  230.             continue;
  231.         }
  232.  
  233.         /*
  234.          * Try to get the date only after using ICMP timestamps to
  235.          * get the time.  This is because the date protocol
  236.          * is optional.
  237.          */
  238.         if (dayaddr.sin_port != 0) {
  239.             dayaddr.sin_family = hp->h_addrtype;
  240.             bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
  241.                   hp->h_length);
  242.             avg = daydiff(*argv);
  243.             if (avg > SECDAY) {
  244.                 printf("time on %s is %ld days ahead %s\n",
  245.                        hp->h_name, avg/SECDAY, myname);
  246.                 continue;
  247.             } else if (avg < -SECDAY) {
  248.                 printf("time on %s is %ld days behind %s\n",
  249.                        hp->h_name, -avg/SECDAY, myname);
  250.                 continue;
  251.             }
  252.         }
  253.  
  254.         if (measure_delta > 0) {
  255.             printf("time on %s is %d ms. ahead of time on %s\n",
  256.                    hp->h_name, measure_delta, myname);
  257.         } else if (measure_delta == 0) {
  258.             printf("%s and %s have the same time\n",
  259.                    hp->h_name, myname);
  260.         } else {
  261.             printf("time on %s is %d ms. behind time on %s\n",
  262.                    hp->h_name, -measure_delta, myname);
  263.         }
  264.     }
  265.     return;
  266. }
  267.  
  268.  
  269. /*
  270.  * finds location of master timedaemon
  271.  */
  272. void
  273. msite(int argc, char *argv[])
  274. {
  275.     int cc;
  276.     fd_set ready;
  277.     struct sockaddr_in dest;
  278.     int i, length;
  279.     struct sockaddr from;
  280.     struct timeval tout;
  281.     struct tsp msg;
  282.     struct servent *srvp;
  283.     char *tgtname;
  284.  
  285.     if (argc < 1) {
  286.         printf("Usage: msite [hostname]\n");
  287.         return;
  288.     }
  289.  
  290.     srvp = getservbyname("timed", "udp");
  291.     if (srvp == 0) {
  292.         fprintf(stderr, "udp/timed: unknown service\n");
  293.         return;
  294.     }
  295.     dest.sin_port = srvp->s_port;
  296.     dest.sin_family = AF_INET;
  297.  
  298.     (void)gethostname(myname, sizeof(myname));
  299.     i = 1;
  300.     do {
  301.         tgtname = (i >= argc) ? myname : argv[i];
  302.         hp = gethostbyname(tgtname);
  303.         if (hp == 0) {
  304.             fprintf(stderr, "timedc: %s: ", tgtname);
  305.             herror(0);
  306.             continue;
  307.         }
  308.         bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
  309.  
  310.         (void)strcpy(msg.tsp_name, myname);
  311.         msg.tsp_type = TSP_MSITE;
  312.         msg.tsp_vers = TSPVERSION;
  313.         bytenetorder(&msg);
  314.         if (sendto(sock, &msg, sizeof(struct tsp), 0,
  315.                (struct sockaddr*)&dest,
  316.                sizeof(struct sockaddr)) < 0) {
  317.             perror("sendto");
  318.             continue;
  319.         }
  320.  
  321.         tout.tv_sec = 15;
  322.         tout.tv_usec = 0;
  323.         FD_ZERO(&ready);
  324.         FD_SET(sock, &ready);
  325.         if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
  326.                &tout)) {
  327.             length = sizeof(struct sockaddr);
  328.             cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
  329.                       &from, &length);
  330.             if (cc < 0) {
  331.                 perror("recvfrom");
  332.                 continue;
  333.             }
  334.             bytehostorder(&msg);
  335.             if (msg.tsp_type == TSP_ACK) {
  336.                 printf("master timedaemon at %s is %s\n",
  337.                        tgtname, msg.tsp_name);
  338.             } else {
  339.                 printf("received wrong ack: %s\n",
  340.                        tsptype[msg.tsp_type]);
  341.             }
  342.         } else {
  343.             printf("communication error with %s\n", tgtname);
  344.         }
  345.     } while (++i < argc);
  346. }
  347.  
  348. /*
  349.  * quits timedc
  350.  */
  351. void
  352. quit()
  353. {
  354.     exit(0);
  355. }
  356.  
  357.  
  358. /*
  359.  * Causes the election timer to expire on the selected hosts
  360.  * It sends just one udp message per machine, relying on
  361.  * reliability of communication channel.
  362.  */
  363. void
  364. testing(int argc, char *argv[])
  365. {
  366.     struct servent *srvp;
  367.     struct sockaddr_in sin;
  368.     struct tsp msg;
  369.  
  370.     if (argc < 2)  {
  371.         printf("Usage: election host1 [host2 ...]\n");
  372.         return;
  373.     }
  374.  
  375.     srvp = getservbyname("timed", "udp");
  376.     if (srvp == 0) {
  377.         fprintf(stderr, "udp/timed: unknown service\n");
  378.         return;
  379.     }
  380.  
  381.     while (argc > 1) {
  382.         argc--; argv++;
  383.         hp = gethostbyname(*argv);
  384.         if (hp == NULL) {
  385.             fprintf(stderr, "timedc: %s: ", *argv);
  386.             herror(0);
  387.             argc--; argv++;
  388.             continue;
  389.         }
  390.         sin.sin_port = srvp->s_port;
  391.         sin.sin_family = hp->h_addrtype;
  392.         bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
  393.  
  394.         msg.tsp_type = TSP_TEST;
  395.         msg.tsp_vers = TSPVERSION;
  396.         (void)gethostname(myname, sizeof(myname));
  397.         (void)strncpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
  398.         bytenetorder(&msg);
  399.         if (sendto(sock, &msg, sizeof(struct tsp), 0,
  400.                (struct sockaddr*)&sin,
  401.                sizeof(struct sockaddr)) < 0) {
  402.             perror("sendto");
  403.         }
  404.     }
  405. }
  406.  
  407.  
  408. /*
  409.  * Enables or disables tracing on local timedaemon
  410.  */
  411. void
  412. tracing(int argc, char *argv[])
  413. {
  414.     int onflag;
  415.     int length;
  416.     int cc;
  417.     fd_set ready;
  418.     struct sockaddr_in dest;
  419.     struct sockaddr from;
  420.     struct timeval tout;
  421.     struct tsp msg;
  422.     struct servent *srvp;
  423.  
  424.     if (argc != 2) {
  425.         printf("Usage: tracing { on | off }\n");
  426.         return;
  427.     }
  428.  
  429.     srvp = getservbyname("timed", "udp");
  430.     if (srvp == 0) {
  431.         fprintf(stderr, "udp/timed: unknown service\n");
  432.         return;
  433.     }
  434.     dest.sin_port = srvp->s_port;
  435.     dest.sin_family = AF_INET;
  436.  
  437.     (void)gethostname(myname,sizeof(myname));
  438.     hp = gethostbyname(myname);
  439.     bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
  440.  
  441.     if (strcmp(argv[1], "on") == 0) {
  442.         msg.tsp_type = TSP_TRACEON;
  443.         onflag = ON;
  444.     } else {
  445.         msg.tsp_type = TSP_TRACEOFF;
  446.         onflag = OFF;
  447.     }
  448.  
  449.     (void)strcpy(msg.tsp_name, myname);
  450.     msg.tsp_vers = TSPVERSION;
  451.     bytenetorder(&msg);
  452.     if (sendto(sock, &msg, sizeof(struct tsp), 0,
  453.            (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
  454.         perror("sendto");
  455.         return;
  456.     }
  457.  
  458.     tout.tv_sec = 5;
  459.     tout.tv_usec = 0;
  460.     FD_ZERO(&ready);
  461.     FD_SET(sock, &ready);
  462.     if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) {
  463.         length = sizeof(struct sockaddr);
  464.         cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
  465.                   &from, &length);
  466.         if (cc < 0) {
  467.             perror("recvfrom");
  468.             return;
  469.         }
  470.         bytehostorder(&msg);
  471.         if (msg.tsp_type == TSP_ACK)
  472.             if (onflag)
  473.                 printf("timed tracing enabled\n");
  474.             else
  475.                 printf("timed tracing disabled\n");
  476.         else
  477.             printf("wrong ack received: %s\n",
  478.                         tsptype[msg.tsp_type]);
  479.     } else
  480.         printf("communication error\n");
  481. }
  482.  
  483. int
  484. priv_resources()
  485. {
  486.     int port;
  487.     struct sockaddr_in sin;
  488.  
  489.     sock = socket(AF_INET, SOCK_DGRAM, 0);
  490.     if (sock < 0) {
  491.         perror("opening socket");
  492.         return(-1);
  493.     }
  494.  
  495.     sin.sin_family = AF_INET;
  496.     sin.sin_addr.s_addr = 0;
  497.     for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
  498.         sin.sin_port = htons((u_short)port);
  499.         if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
  500.             break;
  501.         if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
  502.             perror("bind");
  503.             (void) close(sock);
  504.             return(-1);
  505.         }
  506.     }
  507.     if (port == IPPORT_RESERVED / 2) {
  508.         fprintf(stderr, "all reserved ports in use\n");
  509.         (void) close(sock);
  510.         return(-1);
  511.     }
  512.  
  513.     sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  514.     if (sock_raw < 0)  {
  515.         perror("opening raw socket");
  516.         (void) close(sock);
  517.         return(-1);
  518.     }
  519.     return(1);
  520. }
  521.